package com.icss.ah.productGroup.action;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.j2ee.util.UUID;

/**
 * 相似商品组功能控制类
 * @author xuyl
 * @date 2017-5-31
 */
@Controller
public class ProductGroupAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private String groupName;	//组名
	private String groupUuid;	//id
	private String rangeStart;  //排序范围起始
	private String rangeEnd;	//排序范围截止
	private String status;		//状态
	private String sortNum;		//排序
	private String uuids;		//批量ids
	private String uuid;		//id
	private String productId;	//商品id
	private String proGroupName;//商品组内名称
	private String productName; //商品名称

	/**
	 * 商品组列表页
	 */
	public String toProductGroupListPage(){
		return "toProductGroupListPage";
	}
	
	/**
	 * 商品组列表页查询
	 */
	public void queryProductGroupList(){
		Map<String, String> map = getBase();
		map.put("groupName", groupName);
		map.put("status", status);
		map.put("rangeStart", rangeStart);
		map.put("rangeEnd", rangeEnd);
		map.put("startIndex", getStartIndexMysql());
		map.put("rowNums", getEndIndexMysql());
		dataGridJson = productGroupService.queryProductGroupList(map);
		str2Resp(JSONObject.fromObject(dataGridJson).toString());
	}
	
	/**
	 * 相似商品组新增/编辑页
	 */
	public String toProductGroupPage(){
		request = getRequest();
		request.setAttribute("flag", request.getParameter("flag"));
		request.setAttribute("uuid", uuid);
		request.setAttribute("bean", productGroupService.queryProductGroupInfo(uuid));
		return "toProductGroupPage";
	}
	
	/**
	 * 相似商品组信息保存
	 */
	public void saveProductGroupInfo(){
		String result = "";
		Map<String, String> map = getBase();
		map.put("status", status);
		map.put("sortNum", sortNum);
		map.put("groupName", groupName);
		if(uuid != null && !uuid.equals("")){
			map.put("uuid", uuid);
			result = productGroupService.updateProductGroupInfo(map);
		}else{
			map.put("uuid", new UUID().toString());
			result = productGroupService.saveProductGroupInfo(map);
		}
		str2Resp(result);
	}
	
	/**
	 * 相似商品组信息删除(支持批量删除)
	 */
	public void delProductGroupInfo(){
		String result = productGroupService.delProductGroupInfo(uuids);
		str2Resp(result);
	}
	
	/**
	 * 相似商品组增加相似商品页
	 */
	public String toProductPage(){
		getRequest().setAttribute("uuid", uuid);
		getRequest().setAttribute("bean", productGroupService.queryProductGroupInfo(uuid));
		return "toProductPage";
	}
	
	/**
	 * 商品组相似商品列表页查询
	 */
	public void queryProductList(){
		Map<String, String> map = getBase();
		map.put("proGroupName", proGroupName);
		map.put("uuid", uuid);
		map.put("status", status);
		map.put("rangeStart", rangeStart);
		map.put("rangeEnd", rangeEnd);
		map.put("startIndex", getStartIndexMysql());
		map.put("rowNums", getEndIndexMysql());
		dataGridJson = productGroupService.queryProductList(map);
		str2Resp(JSONObject.fromObject(dataGridJson).toString());
	}

	/**
	 * 相似商品组编辑商品信息
	 */
	public void updateProduct(){
		Map<String, String> map = getBase();
		map.put("uuid", uuid);
		map.put("sortNum", sortNum);
		map.put("status", status);
		map.put("proGroupName", proGroupName);
		String result = productGroupService.updateProduct(map);
		str2Resp(result);
	}
	
	/**
	 * 相似商品组删除商品信息(支持批量删除)
	 */
	public void delProductInfo(){
		String result = productGroupService.delProduct(uuids);
		str2Resp(result);
	}
	
	/**
	 * 商品列表页
	 */
	public String toProductInfoPage(){
		System.out.println("=======groupUuid===="+groupUuid);
		getRequest().setAttribute("groupUuid", groupUuid);
		return "toProductInfoPage";
	}
	
	/**
	 * 商品列表页查询
	 */
	public void queryProductInfoList(){
		Map<String, String> map = getBase();
		map.put("productName", productName);
		map.put("startIndex", getStartIndexMysql());
		map.put("rowNums", getEndIndexMysql());
		dataGridJson = productGroupService.queryProductInfoList(map);
		str2Resp(JSONObject.fromObject(dataGridJson).toString());
	}
	
	/**
	 * 相似商品组增加相似商品信息
	 */
	public void saveProductInfo(){
		String result = productGroupService.saveProductInfo(getBase(),uuids,groupUuid);
		str2Resp(result);
	}

	/*
	 * 基础信息
	 */
	public Map<String, String> getBase(){
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String shopId=userInfo.getShopId();
		Map<String, String> map = new HashMap<String, String>();
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		map.put("userCode", userCode);
		return map;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getRangeStart() {
		return rangeStart;
	}

	public void setRangeStart(String rangeStart) {
		this.rangeStart = rangeStart;
	}

	public String getRangeEnd() {
		return rangeEnd;
	}

	public void setRangeEnd(String rangeEnd) {
		this.rangeEnd = rangeEnd;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getSortNum() {
		return sortNum;
	}

	public void setSortNum(String sortNum) {
		this.sortNum = sortNum;
	}

	public String getUuids() {
		return uuids;
	}

	public void setUuids(String uuids) {
		this.uuids = uuids;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProGroupName() {
		return proGroupName;
	}

	public void setProGroupName(String proGroupName) {
		this.proGroupName = proGroupName;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getGroupUuid() {
		return groupUuid;
	}

	public void setGroupUuid(String groupUuid) {
		this.groupUuid = groupUuid;
	}
	
}
